package com.lgl.scsapi.resolver;

import com.lgl.scsapi.util.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.PoolException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 通用异常处理类
 */
public class MyExceptionResolver implements HandlerExceptionResolver{
    private Logger log = LoggerFactory.getLogger(MyExceptionResolver.class);

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception e) {
		//如果是redis连接异常
		if (e instanceof PoolException) {
			Constants.REDIS_IS_USABLE = false;
		}
		// TODO Auto-generated method stub
		log.info("==============异常开始=============");
        log.error(e.toString(),e);
		log.info("==============异常结束=============");
		//ModelAndView mv = new ModelAndView("error");
		ModelAndView mv = new ModelAndView("staticFile/errorPages/500.html");
		mv.addObject("exception", e.toString().replaceAll("\n", "<br/>"));
		return mv;
	}

}
